package service

import (
	"BiYeProServer/src/model"
	"BiYeProServer/src/model/dao"
	"BiYeProServer/src/util"
	"fmt"
	"github.com/gin-gonic/gin"
	"strconv"
	"time"
)

func SaveCode(ctx *gin.Context)  {
	var code model.Code
	err := ctx.Bind(&code)
	if err != nil || code.Username == ""{
		util.SetResponse(ctx,5009,false,nil,"代码参数上传错误")
		return
	}
	// 同一个人同一道题的情况待定,先直接保存
	//c := &model.Code{}
	//err = dao.GetDb().Where("user_id = ? and author_id = ?",code.UserId,code.AuthorId).First(&c).Error
	//if err != nil{
	//	util.SetResponse(ctx,5010,false,nil,"数据库操作错误")
	//	return
	//}
	if code.Id != 0{
		err = dao.GetDb().Save(&code).Error
		if err != nil{
			util.SetResponse(ctx,5010,false,nil,"数据库操作错误")
			return
		}
		//code.Code = ""
		//util.SetResponse(ctx,200,true,code,"保存成功")
		//return
	}else {
		err = dao.GetDb().Create(&code).Error
	}
	if err != nil{
		util.SetResponse(ctx,5010,false,nil,"数据库操作错误")
		return
	}
	go func() {
		solution := ChangeCode2Solution(ctx,code)
		err := dao.GetDb().Create(&solution).Error
		if err != nil{
			fmt.Println("数据库操作错误5015")
			return
		}
		_ = dao.GetDb().Create(&model.SourceCode{SolutionId: solution.SolutionId,Source: code.Code}).Error
		err = dao.GetDb().Create(&model.SourceCodeUser{SolutionId: solution.SolutionId,Source: code.Code}).Error
		if err != nil{
			fmt.Println("数据库操作错误5015")
			return
		}
	}()
	util.SetResponse(ctx,200,true,code,"保存成功")
}

func ChangeCode2Solution(ctx  *gin.Context,code model.Code) *model.Solution {
	sol := &model.Solution{
		ProblemId: code.QuestionId,
		UserId: "admin",
		Nick: "admin",
		InDate: time.Now(),
		Language:GetLangByType(code.Type),
		Ip: "223.75.69.41",
		ContestId: 0,
		Valid: 1,
		Num: -1,
		CodeLength: len(code.Code),
		Judgetime: time.Now(),
		LintError: "0",
		Judger: "admin",
		CodeId: code.Id,
	}
	return sol
}

func GetRequestIP(c *gin.Context)string{
	reqIP := c.ClientIP()
	fmt.Println(reqIP,"this is ip")
	if reqIP == "::1" {
		reqIP = "127.0.0.1"
	}
	return reqIP
}

func GetLangByType(Type string) int {
	if Type == "c"{
		return 0
	}
	if Type == "cpp"{
		return 1
	}

	if Type == "java"{
		return 2
	}
	if Type == "python"{
		return 3
	}
	if Type == "php"{
		return 4
	}
	if Type == "c#"{
		return 5
	}
	if Type == "js"{
		return 6
	}
	if Type == "go"{
		return 7
	}
	return -1
}

func GetCodes(ctx *gin.Context)  {
	uid := ctx.Query("user_id")
	userId,err := strconv.ParseInt(uid,10,64)
	if err != nil || userId == 0{
		util.SetResponse(ctx,5011,false,nil,"参数传递错误")
		return
	}
	var codes []model.Code
	err = dao.GetDb().Select(util.SubtractArray(model.CodeColumn,"code")).Where("user_id = ?",userId).Find(&codes).Limit(10).Error
	if err != nil{
		util.SetResponse(ctx,5012,false,nil,"数据库操作错误")
		return
	}
	util.SetResponse(ctx,200,true,codes,"操作成功")
}

func GetCodeById(ctx *gin.Context)  {
	iid := ctx.Query("id")
	id,err := strconv.ParseInt(iid,10,64)
	if err != nil || id == 0{
		util.SetResponse(ctx,5013,false,nil,"参数传递错误")
		return
	}
	var code model.Code
	err = dao.GetDb().Where("id=?",id).Find(&code).Error
	if err != nil{
		util.SetResponse(ctx,5014,false,nil,"数据库操作错误")
		return
	}
	util.SetResponse(ctx,200,true,code,"查询成功")
}

const  runCodeUrl = "http://81.70.78.200/submit.php"
func RunCode(Type,Code string,id uint64)  {
	util.Post(runCodeUrl, map[string]string{
		"id":fmt.Sprintf("%d",id),
		"language":GetLang(Type),
		"source":Code,
	})
}

func GetLang(Type string) string {
	if Type == "c"{
		return "0"
	}

	return "0"
}
